package com.example.sqlite;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DataBaseHelp extends SQLiteOpenHelper {
    public static final String COPY_TEXT_TABLE = "COPY_TEXT_TABLE";
    public static final String COLUMN_TEXT = "TEXT";
    public static final String COLUMN_ID = "ID";

    public DataBaseHelp(@Nullable Context context) {
        super(context, "copy-text.db", null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTableStatement = "CREATE TABLE " + COPY_TEXT_TABLE +
                " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_TEXT + " TEXT)";
        db.execSQL(createTableStatement);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public boolean addOne(CopyTextModel copyTextModel) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

//        System.out.println("text:"+copyTextModel.getText());
        try {
            cv.put(COLUMN_TEXT, copyTextModel.getText().trim());
            long insert = db.insert(COPY_TEXT_TABLE, null, cv);
            return insert != -1;
        } catch (Exception e) {

            return false;
        }
    }

    public boolean deleteOne(CopyTextModel copyTextModel) {
        SQLiteDatabase db = this.getWritableDatabase();
        String queryString = "DELETE FROM " + COPY_TEXT_TABLE + " WHERE " + COLUMN_ID + " = ?";

        @SuppressLint("Recycle")
        Cursor cursor = db.rawQuery(queryString, new String[]{String.valueOf(copyTextModel.getId())});
        return cursor.moveToFirst();
    }


    public CopyTextModel getClassByText(String clickedText) {
        //TODO
        System.out.println("click:" + clickedText);
        SQLiteDatabase db = this.getWritableDatabase();
        CopyTextModel newCopyText = null;
        //too long string need to surround by a pair of Single quotation mark'
        String queryString = "SELECT * FROM " + COPY_TEXT_TABLE + " WHERE " +
                COLUMN_TEXT + " = " + "?";
        //请使用预编译sql
        Cursor cursor = db.rawQuery(queryString, new String[]{clickedText});
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(0);
                String text = cursor.getString(1);
                newCopyText = new CopyTextModel();
                newCopyText.setText(text);
                newCopyText.setId(id);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return newCopyText;
    }

    /*
    public String getText(CopyTextModel copyTextModel) {
        SQLiteDatabase db = this.getWritableDatabase();
        String queryString = "SELECT " + COLUMN_TEXT + " FROM " + COPY_TEXT_TABLE + " WHERE " +
                COLUMN_ID + " = " + copyTextModel.getId();

        String text = "";
        Cursor cursor = db.rawQuery(queryString, null);
        if (cursor.moveToFirst()) {
            do {
                text = cursor.getString(0);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return text;
    }*/

    public List<String> getTextList() {
        List<String> textList = new ArrayList<>();
        String queryString = "SELECT * FROM " + COPY_TEXT_TABLE;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(queryString, null);
        if (cursor.moveToFirst()) {
            do {
                String text = cursor.getString(1);
                textList.add(text);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        Collections.reverse(textList);
        return textList;
    }
}
